import java.io.*;
import java.util.*;
import java.net.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;

public class GetCompanyProfile
{
    private static Vector vInput= new Vector(10);
    private static String symbol;
    private static String input = new String();


    public static void main(String[] args) {

	GetCompanyProfile gProfile = new GetCompanyProfile();
	gProfile.connectToUrl("http://biz.yahoo.com/p/s/sons.html");
	vInput = gProfile.putInVectorByLine (input);
	vInput = gProfile.selectUsefulInfo (vInput);
	gProfile.getEventMessage( vInput);
    }

    public void connectToUrl (String urlAddress) {

        URL url = null;        
	HttpURLConnection httpConnection = null;

	try {            
	    url = new URL (urlAddress);  
	    try {                
		httpConnection = (HttpURLConnection)url.openConnection();                
		httpConnection.setDoOutput(false);                
		httpConnection.connect();                

		System.out.println("GettingCompanyProfile:" + 
				   httpConnection.getResponseCode() + 
				   " - " + httpConnection.getResponseMessage()+"\n");
		if (httpConnection.getResponseCode() != 200) {
		    input = null;
		  
		    System.out.println("Can not find profile from yahoo!");
		    return;
		}

		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpConnection.getInputStream()));

		StringBuffer buff1= new StringBuffer("");

		input = in.readLine();

		while (input != null) {
		    buff1.append(input+"\n");
		    input =  in.readLine();
		}

		in.close();            

		input =  buff1.toString();
		input=input.replaceAll("\n"," ");
		input=input.replaceAll("<br>"," ");
		input=input.replaceAll("&nbsp;"," ");
		input=input.replaceAll("<","\n<");
		input=input.replaceAll(">",">\n");
		input=input.replaceAll("</","\n</");
		//input=input.replaceAll("\n"," ");

	
	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public String getUrlReturn () {
	return input;
    }

    public Vector putInVectorByLine (String input) {

	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		//System.out.println(element);
		v.addElement(element);
	    }
	}
	
	return v;
    }

    public Vector selectUsefulInfo (Vector v_in) {

	Vector v_out = new Vector (10);
	String text = "";
	Enumeration penum = v_in.elements();
	
	while ( penum.hasMoreElements()) {

	    text = (String)penum.nextElement();
	    if (text.startsWith("Financials")) {
		break;
	    }
	}
       
	while ( penum.hasMoreElements() ) {
	    text = ((String)penum.nextElement());
	    if (!( text.startsWith("<") || 
		   text.startsWith("|") ||
		   text.startsWith("&") ||
		   text.startsWith(":") ||  
		   text.startsWith("(") )) {

		v_out.addElement(text);
	    }
	}

	return v_out;
    }

   public String getEventMessage ( Vector inputV) {

       String temp=""; String message="";
       Enumeration penum = inputV.elements();
       while ( penum.hasMoreElements()) { 
	   temp =(String) penum.nextElement();
	   
	   if (temp.equals("Location")) {
	       break;
	   }
	   
	   message += (temp + " ");
	}
       return message;
    }
}






